package com.mbc.rmt.manager;

import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRResultSetDataSource;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.util.JRLoader;

import com.mbc.common.CommonSingleton;
import com.mbc.common.dao.CommonDAOFactory;
import com.mbc.common.dao.CurrencyRateDAO;
import com.mbc.common.dao.CurrencyRateDetailDAO;
import com.mbc.common.dao.LogDAO;
import com.mbc.common.entities.CurrencyRate;
import com.mbc.common.entities.CurrencyRateDetail;
import com.mbc.common.entities.Log;
import com.mbc.common.exception.DataDuplicationException;
import com.mbc.common.exception.DataNotFoundException;
import com.mbc.common.manager.AbstractMgr;
import com.mbc.common.utils.KeyGenerator;
import com.mbc.rmt.dao.BankDAO;
import com.mbc.rmt.dao.BranchBalanceDAO;
import com.mbc.rmt.dao.BranchCOADAO;
import com.mbc.rmt.dao.BranchCashTransactionDAO;
import com.mbc.rmt.dao.General_COADAO;
import com.mbc.rmt.dao.RemittanceDAOFactory;
import com.mbc.rmt.data.BranchCashTransactionDataList;
import com.mbc.rmt.entities.Bank;
import com.mbc.rmt.entities.BranchBalance;
import com.mbc.rmt.entities.BranchCOA;
import com.mbc.rmt.entities.BranchCashTransaction;
import com.mbc.rmt.entities.BranchCashTransactionRpt;
import com.mbc.rmt.entities.GeneralLedger;
import com.mbc.rmt.entities.General_COA;

public class BranchCashTransactionMgr extends AbstractMgr{
	
	private BranchCashTransactionDAO branchcashtransactionDAO;	
	private BranchBalanceDAO branchbalanceDAO;
	private CurrencyRateDetailDAO currencyrateDetailDAO;
	private BranchCOADAO branchCOADAO;
	private CurrencyRateDAO currencyRateDAO;
	private General_COADAO generalCOADAO;
	private BankDAO bankDAO;
	private LogDAO logDAO;
	
	public static enum GL_STATUS { CREATE, DELETE }
	
	public BranchCashTransactionMgr() {
	}
	
	public BranchCashTransactionMgr(String sessionId) {
		this.sessionId = sessionId;
	}
	
	public String create(BranchCashTransaction entity) throws Exception {
		openConnection();
		try {
			beginTransaction();
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager.getConnection());
			branchbalanceDAO = RemittanceDAOFactory.getBranchBalanceDAO(dbManager.getConnection());			
			currencyrateDetailDAO = CommonDAOFactory.getCurrencyRateDetailDAO(dbManager.getConnection());
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			
			String voucher = "";
			
			if(entity.getSlipid() == 0){
				BranchCashTransaction l_info = new BranchCashTransaction();
				l_info = branchcashtransactionDAO.find(entity.getSlipid());			
				if (l_info != null) {
					throw new Exception("Slip ID already exists!");
				} 
				else 
				{						
					CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();					
					l_currencyrateDetail = currencyrateDetailDAO.findLatest(entity.getCurrency_rate_Id());
					
					if (l_currencyrateDetail.getRate()  !=  entity.getRate()){					  
				   
					   l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
					   l_currencyrateDetail.setCurrencyRateId(entity.getCurrency_rate_Id());
					   l_currencyrateDetail.setRate(entity.getRate());
					   l_currencyrateDetail.seteDateTime(entity.getSlipdate());
					   currencyrateDetailDAO.create(l_currencyrateDetail);			
					   
					   l_logentity.setTablename("CurrencyRateDetail");
					   l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					   l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
					   l_logentity.setLogstatus(Log.__SAVE_DATA);
					   logDAO.getData(l_logentity);
					}
					
					entity.setSlipid(KeyGenerator.generateKey(getConnection()));
					if(entity.getTransactiontype() == 0)
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_RECEIVED_FROM_BRANCH, entity.getSlipdate(), entity.getDefaultBranchID(), getConnection());
					else
						voucher = RemittanceSerialMgr.getFormattedSerial(RemittanceSerialMgr.CASH_PAID_TO_BRANCH, entity.getSlipdate(), entity.getDefaultBranchID(), getConnection());
					
					entity.setSlipcode(voucher);
					
					gotoGL(entity, GL_STATUS.CREATE);
					branchcashtransactionDAO.create(entity);	
					
					l_logentity.setTablename("BranchCashTransaction");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(entity.getSlipid());
					l_logentity.setUserid(entity.getCreateduser());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
					
					BranchBalance l_balance = new BranchBalance();					
					l_balance.setCurrencyid(entity.getCurrency_Id());
					l_balance.setBranch_id(entity.getBranchid());
					l_balance = branchbalanceDAO.getInfo(l_balance);					
					
					if(l_balance == null)
					{
						l_balance = new BranchBalance();
						l_balance.setBranchbalance_id(KeyGenerator.generateKey(getConnection()));
						l_balance.setBranch_id(entity.getBranchid());
						l_balance.setCurrencyid(entity.getCurrency_Id());
						l_balance.setOpeningbalance(0.0);
						l_balance.setCurrentbalance(entity.getReceivedamt());						
						
						if(entity.getTransactiontype() == 0){
							//l_balance.setOpeningbalance(entity.getReceivedamt());
							l_balance.setCurrentbalance(-(entity.getReceivedamt()));
						}else{
							//l_balance.setOpeningbalance(-(entity.getReceivedamt()));
							l_balance.setCurrentbalance(entity.getReceivedamt());
						}
						branchbalanceDAO.create(l_balance);		
						
					}
					else
					{
						if(entity.getTransactiontype() == 0){
							//l_balance.setOpeningbalance(l_balance.getOpeningbalance() + entity.getReceivedamt());
							l_balance.setCurrentbalance(l_balance.getCurrentbalance() - entity.getReceivedamt());
						}else {
							//l_balance.setOpeningbalance(l_balance.getOpeningbalance() - entity.getReceivedamt());
							l_balance.setCurrentbalance(l_balance.getCurrentbalance() + entity.getReceivedamt());
						}
						branchbalanceDAO.update(l_balance);					
					}
				}
			}
			else{
				
				branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager.getConnection());
				branchcashtransactionDAO.update(entity);
				
				l_logentity.setTablename("BranchCashTransaction");
				l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
				l_logentity.setKey(entity.getSlipid());
				l_logentity.setUserid(entity.getModifieduser());
				l_logentity.setLogstatus(Log.__UPDATE_DATA);
				logDAO.getData(l_logentity);
				
				gotoGL(entity, GL_STATUS.CREATE);
				CurrencyRateDetail l_currencyrateDetail = new CurrencyRateDetail();					
				l_currencyrateDetail = currencyrateDetailDAO.findLatest(entity.getCurrency_rate_Id());
				
				if (l_currencyrateDetail.getRate()  !=  entity.getRate()){					  
			   
				   l_currencyrateDetail.setCurrencyRateDetailId(KeyGenerator.generateKey(getConnection()));
				   l_currencyrateDetail.setCurrencyRateId(entity.getCurrency_rate_Id());
				   l_currencyrateDetail.setRate(entity.getRate());
				   l_currencyrateDetail.seteDateTime(entity.getSlipdate());
				   currencyrateDetailDAO.create(l_currencyrateDetail);	
				   
				   l_logentity.setTablename("CurrencyRateDetail");
					l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
					l_logentity.setKey(l_currencyrateDetail.getCurrencyRateDetailId());
					l_logentity.setLogstatus(Log.__SAVE_DATA);
					logDAO.getData(l_logentity);
				}				
				
				BranchBalance l_balance = new BranchBalance();
				
				double npamt = 0;
				double tempnpamt = 0;
				BranchCashTransaction temp_entity = new BranchCashTransaction();
				temp_entity = branchcashtransactionDAO.getBranchCashTransaction(entity);
				npamt = entity.getReceivedamt();			
				tempnpamt =  temp_entity.getReceivedamt();
				long currencyid = entity.getCurrency_Id();
				long tempcurrencyid = temp_entity.getCurrency_Id();
				long branchid = entity.getBranchid();
				long tempbranchid = temp_entity.getBranchid();
				
				if(tempcurrencyid == currencyid &&  tempbranchid == branchid)
				{
					l_balance.setCurrencyid(entity.getCurrency_Id());
					l_balance.setBranch_id(entity.getBranchid());
					l_balance = branchbalanceDAO.getInfo(l_balance);
																		
					double amt = 0;
					
					if(npamt > tempnpamt){
						amt = tempnpamt - npamt;
						if(entity.getTransactiontype() == 0){
							//l_balance.setOpeningbalance(l_balance.getOpeningbalance() - amt);
							l_balance.setCurrentbalance(l_balance.getCurrentbalance() + amt);
						}							
						else{
							//l_balance.setOpeningbalance(l_balance.getOpeningbalance() + amt);
							l_balance.setCurrentbalance(l_balance.getCurrentbalance() - amt);
						}						
					}
					else if(npamt < tempnpamt){										
						amt = tempnpamt - npamt;
						if(entity.getTransactiontype() == 0){
							//l_balance.setOpeningbalance(l_balance.getOpeningbalance() - amt);
							l_balance.setCurrentbalance(l_balance.getCurrentbalance() + amt);
						}							
						else{
							//l_balance.setOpeningbalance(l_balance.getOpeningbalance() + amt);
							l_balance.setCurrentbalance(l_balance.getCurrentbalance() - amt);
						}						
					}
					else{
						//l_balance.setOpeningbalance(l_balance.getOpeningbalance());
						l_balance.setCurrentbalance(l_balance.getCurrentbalance());
					}
				}
				else
				{
					double amount = 0;
					l_balance.setCurrencyid(entity.getTempcurrencyid());
					l_balance.setBranch_id(entity.getTempbranchid());
					l_balance = branchbalanceDAO.getInfo(l_balance);	
					
					if(entity.getTransactiontype() == 0){
						//amount = l_balance.getOpeningbalance() - tempnpamt;
						amount = l_balance.getCurrentbalance() + tempnpamt;
					}else{
						//amount = l_balance.getOpeningbalance() + tempnpamt;
						amount = l_balance.getCurrentbalance() - tempnpamt;
					}
					l_balance.setOpeningbalance(amount);
					l_balance.setCurrentbalance(amount);
					branchbalanceDAO.update(l_balance);
					
					l_balance.setCurrencyid(entity.getCurrency_Id());
					l_balance.setBranch_id(entity.getBranchid());
					l_balance = branchbalanceDAO.getInfo(l_balance);
										
					if(l_balance == null)
					{						
						l_balance = new BranchBalance();
						l_balance.setBranchbalance_id(KeyGenerator.generateKey(getConnection()));
						l_balance.setBranch_id(entity.getBranchid());
						l_balance.setCurrencyid(entity.getCurrency_Id());
						if(entity.getTransactiontype() == 0)
							l_balance.setOpeningbalance(0.0);
						else
							l_balance.setOpeningbalance(0.0);
						if(entity.getTransactiontype() == 0 )
							l_balance.setCurrentbalance(-(npamt));
						else
							l_balance.setCurrentbalance(npamt);
						
						branchbalanceDAO.create(l_balance);
					
					}
					else
					{
						if(entity.getTransactiontype() == 0)
						{
							if(entity.getTempcurrencyid() == entity.getCurrency_Id() && entity.getTempbranchid() == entity.getBranchid()){
								//l_balance.setOpeningbalance(amount + npamt);		
								l_balance.setCurrentbalance(amount - npamt);
							}
							else{		
								//l_balance.setOpeningbalance(l_balance.getOpeningbalance() + npamt);
								l_balance.setCurrentbalance(l_balance.getCurrentbalance() - npamt);
							}
						}
						else
						{
							if(entity.getTempcurrencyid() == entity.getCurrency_Id() && entity.getTempbranchid() == entity.getBranchid()){
								//l_balance.setOpeningbalance(amount - npamt);		
								l_balance.setCurrentbalance(amount + npamt);
							}
							else{		
								//l_balance.setOpeningbalance(l_balance.getOpeningbalance() - npamt);
								l_balance.setCurrentbalance(l_balance.getCurrentbalance() + npamt);
							}
						}	
					}						
				}
				branchbalanceDAO.update(l_balance);			
		}
		commintTransaction();
		return voucher;
	} catch (SQLException e) {
		if (DataDuplicationException.UNIQUE_KEY_ERROR_CODE == e
				.getErrorCode())
			throw new DataDuplicationException(e.getMessage());		
			rollbackTransaction();
		throw e;
	} finally {
		closeConnection();
	}
}
	
	public BranchCashTransaction find(long key) throws Exception {
		openConnection();
		try {
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager
					.getConnection());
			return branchcashtransactionDAO.find(key);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public BranchCashTransactionDataList findBranchCashTranDataList(BranchCashTransaction cri)
			throws Exception {
		openConnection();
		try {
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager
					.getConnection());
			return branchcashtransactionDAO.findBranchCashTranDataList(cri);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	
	
	public BranchCashTransaction getBranchCashTransaction(BranchCashTransaction entity) throws Exception {
		openConnection();
		try {
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager.getConnection());			
			return branchcashtransactionDAO.getBranchCashTransaction(entity);
			
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
	
	public void delete(BranchCashTransaction entity,long userId, Date deletedDate) throws Exception {
		openConnection();
		try {
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager.getConnection());			
			branchbalanceDAO = RemittanceDAOFactory.getBranchBalanceDAO(dbManager.getConnection());	
			
			logDAO = CommonDAOFactory.getLogDAO(getConnection());
			Log l_logentity = new Log();
			l_logentity.setTablename("BranchCashTransaction");
			l_logentity.setLogkey(KeyGenerator.generateKey(getConnection()));
			l_logentity.setKey(entity.getSlipid());
			l_logentity.setUserid(userId);
			l_logentity.setLogstatus(Log.__DELETE_DATA);
			logDAO.getData(l_logentity);
			
			gotoGL(entity, GL_STATUS.DELETE);
			branchcashtransactionDAO.delete(entity);
		
			BranchBalance l_balance = new BranchBalance();
			l_balance.setCurrencyid(entity.getCurrency_Id());
			l_balance.setBranch_id(entity.getBranchid());
			l_balance = branchbalanceDAO.getInfo(l_balance);
			
			if(l_balance != null){
				l_balance.setBranch_id(entity.getBranchid());
				l_balance.setCurrencyid(entity.getCurrency_Id());
				if(entity.getTransactiontype() == 0){
					//l_balance.setOpeningbalance(l_balance.getOpeningbalance() - entity.getReceivedamt());
					l_balance.setCurrentbalance(l_balance.getCurrentbalance() + entity.getReceivedamt());
				}else{
					//l_balance.setOpeningbalance(l_balance.getOpeningbalance() + entity.getReceivedamt());
					l_balance.setCurrentbalance(l_balance.getCurrentbalance() - entity.getReceivedamt());			
				}
				branchbalanceDAO.update(l_balance);
			}		
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}	
	
	public BranchCashTransaction findByCode(String code, long branchId)throws Exception{
		openConnection();
		try {
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager.getConnection());
			return branchcashtransactionDAO.findByCode(code, branchId);
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}
	}
//goto GL
public void gotoGL(BranchCashTransaction l_entity, GL_STATUS status) throws Exception {
try{	
		currencyRateDAO = CommonDAOFactory.getCurrencyRateDAO(getConnection());
		branchCOADAO = RemittanceDAOFactory.getBranchCOADAO(getConnection());	
		generalCOADAO = RemittanceDAOFactory.getGeneral_COADAO(getConnection());		
		bankDAO = RemittanceDAOFactory.getBankDAO(getConnection());
		
		GeneralLedgerMgr l_glmgr = new GeneralLedgerMgr(this.sessionId);
		if(status == GL_STATUS.DELETE){
			if(l_entity.getTransactiontype() == 0)
				l_glmgr.deleteGL(l_entity.getSlipcode() + "-RB");
			else
				l_glmgr.deleteGL(l_entity.getSlipcode() + "-PB");
		}
		
		if(l_entity.getTransactiontype() == 0)
			l_glmgr.deleteGL(l_entity.getSlipcode() + "-RB");
		else
			l_glmgr.deleteGL(l_entity.getSlipcode() + "-PB");
		
		if(status == GL_STATUS.CREATE) {			
			BranchCOA l_branchCOA = branchCOADAO.findByHostBranchID(l_entity.getDefaultBranchID(), l_entity.getBranchid());
			General_COA l_generalCOA = generalCOADAO.find(l_entity.getDefaultBranchID());
			Bank l_Bank = new Bank();
			if(l_entity.getBankid() != null){
				l_Bank = bankDAO.findByCode(l_entity.getBankcode(), l_entity.getDefaultBranchID());
			}
			
			if(l_branchCOA == null)
				throw new DataNotFoundException("Plz fill BranchCOA", BranchCOA.class);
			
			GeneralLedger _glHeader = new GeneralLedger();
			//_glHeader.setGlKey(KeyGenerator.generateKey(getConnection()));
			_glHeader.setGlKey(l_glmgr.getGLKey());
			if(l_entity.getTransactiontype() == 0)
				_glHeader.setSlip_ID(l_entity.getSlipcode() + "-RB");
			else
				_glHeader.setSlip_ID(l_entity.getSlipcode() + "-PB");
			_glHeader.setSlipDate(l_entity.getSlipdate());
			_glHeader.setSystemDate(new Date());
			_glHeader.setStatus("JH");
			_glHeader.setSrNo(1);
			_glHeader.setOffice_ID(l_entity.getDefaultBranchID());			

			CurrencyRate l_currencyrate = currencyRateDAO.getCurrencyRateFromSourcetoHome(l_entity.getCurrency_Id(), l_entity.getDefaultBranchID());
			
			if(l_entity.getTransactiontype() == 0){
				GeneralLedger _glDebit = new GeneralLedger();
				//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glDebit.setGlKey(l_glmgr.getGLKey());
				_glDebit.setSlip_ID(l_entity.getSlipcode() + "-RB");
				_glDebit.setSlipDate(l_entity.getSlipdate());
				_glDebit.setSystemDate(new Date());
				if(l_entity.getBankid() == null){
					_glDebit.setCoaKey(l_generalCOA.getCashAccountID());
					_glDebit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
				}else{
					_glDebit.setCoaKey(l_Bank.getCoaid());
					_glDebit.setDepartmentKey(l_Bank.getDepartmentid());
				}
				_glDebit.setCurrencyKey(l_entity.getCurrency_Id());
				_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glDebit.setExchangeRate(l_currencyrate.getRate());
				_glDebit.setSourceAmount(l_entity.getReceivedamt());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glDebit.setHomeAmount(l_entity.getReceivedamt() * l_currencyrate.getRate());
				else
					_glDebit.setHomeAmount(l_entity.getReceivedamt() / l_currencyrate.getRate());
				
				_glDebit.setStatus("JRB");	
				_glDebit.setSrNo(1);
				_glDebit.setOffice_ID(l_entity.getDefaultBranchID());			
				l_glmgr.createGL(_glDebit);		
				
				GeneralLedger _glCredit = new GeneralLedger();
				//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glCredit.setGlKey(l_glmgr.getGLKey());
				_glCredit.setSlip_ID(l_entity.getSlipcode() + "-RB");
				_glCredit.setSlipDate(l_entity.getSlipdate());
				_glCredit.setSystemDate(new Date());
				_glCredit.setCoaKey(l_branchCOA.getCurrentAccountID());
				_glCredit.setDepartmentKey(l_branchCOA.getDepartmentID());			
				_glCredit.setCurrencyKey(l_entity.getCurrency_Id());
				_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glCredit.setExchangeRate(l_currencyrate.getRate());
				_glCredit.setSourceAmount(-(l_entity.getReceivedamt()));
				
				if(l_currencyrate.getRoper().equals("M"))
					_glCredit.setHomeAmount(-(l_entity.getReceivedamt() * l_currencyrate.getRate()));
				else
					_glCredit.setHomeAmount(-(l_entity.getReceivedamt() / l_currencyrate.getRate()));
				
				_glCredit.setStatus("JRB");
				_glCredit.setSrNo(2);
				_glCredit.setOffice_ID(l_entity.getDefaultBranchID());
				
				l_glmgr.createGL(_glCredit);
			}else{
				GeneralLedger _glDebit = new GeneralLedger();
				//_glDebit.setGlKey(KeyGenerator.generateKey(getConnection()));			
				_glDebit.setGlKey(l_glmgr.getGLKey());
				_glDebit.setSlip_ID(l_entity.getSlipcode() + "-PB");
				_glDebit.setSlipDate(l_entity.getSlipdate());
				_glDebit.setSystemDate(new Date());
				_glDebit.setCoaKey(l_branchCOA.getCurrentAccountID());
				_glDebit.setDepartmentKey(l_branchCOA.getDepartmentID());	
				_glDebit.setCurrencyKey(l_entity.getCurrency_Id());
				_glDebit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glDebit.setExchangeRate(l_currencyrate.getRate());
				_glDebit.setSourceAmount(l_entity.getReceivedamt());
				
				if(l_currencyrate.getRoper().equals("M"))
					_glDebit.setHomeAmount(l_entity.getReceivedamt() * l_currencyrate.getRate());
				else
					_glDebit.setHomeAmount(l_entity.getReceivedamt() / l_currencyrate.getRate());
				
				_glDebit.setStatus("JPB");	
				_glDebit.setSrNo(1);
				_glDebit.setOffice_ID(l_entity.getDefaultBranchID());			
				l_glmgr.createGL(_glDebit);		
				
				GeneralLedger _glCredit = new GeneralLedger();
				//_glCredit.setGlKey(KeyGenerator.generateKey(getConnection()));
				_glCredit.setGlKey(l_glmgr.getGLKey());
				_glCredit.setSlip_ID(l_entity.getSlipcode() + "-PB");
				_glCredit.setSlipDate(l_entity.getSlipdate());
				_glCredit.setSystemDate(new Date());
				if(l_entity.getBankid() == null){
					_glCredit.setCoaKey(l_generalCOA.getCashAccountID());
					_glCredit.setDepartmentKey(l_generalCOA.getCashDepartmentID());
				}else{
					_glCredit.setCoaKey(l_Bank.getCoaid());
					_glCredit.setDepartmentKey(l_Bank.getDepartmentid());
				}				
				_glCredit.setCurrencyKey(l_entity.getCurrency_Id());
				_glCredit.setCurrencyRateKey(l_currencyrate.getCurrencyRateId());
				_glCredit.setExchangeRate(l_currencyrate.getRate());
				_glCredit.setSourceAmount(-(l_entity.getReceivedamt()));
				
				if(l_currencyrate.getRoper().equals("M"))
					_glCredit.setHomeAmount(-(l_entity.getReceivedamt() * l_currencyrate.getRate()));
				else
					_glCredit.setHomeAmount(-(l_entity.getReceivedamt() / l_currencyrate.getRate()));
				
				_glCredit.setStatus("JPB");
				_glCredit.setSrNo(2);
				_glCredit.setOffice_ID(l_entity.getDefaultBranchID());
				
				l_glmgr.createGL(_glCredit);
			}
			l_glmgr.createGL(_glHeader);
		}
	} catch(Exception e) { throw e; }
}

	//Report
	public JasperPrint prepareBranchCashTransactionReport(BranchCashTransactionRpt info)
			throws Exception {
		openConnection();
		JasperPrint l_jsPrint = null;
		try {
			branchcashtransactionDAO = RemittanceDAOFactory.getBranchCashTransactionDAO(dbManager.getConnection());
			JRResultSetDataSource l_jrRS = new JRResultSetDataSource(branchcashtransactionDAO.prepareBranchCashTransactionReport(info));
			JasperReport l_jsRpt = null;
			if(info.getGroupby().equals("CurrencyID"))
				l_jsRpt = (JasperReport) JRLoader
				.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/BranchCashTransactionforCurRpt.jasper");						
			else
				l_jsRpt = (JasperReport) JRLoader
					.loadObjectFromFile(CommonSingleton.ABSOLUTE_PATH +"reports/BranchCashTransactionRpt.jasper");
	
			HashMap<String, Object> l_params = new HashMap<String, Object>();
			l_params.put("companyname", "mbc");
			l_jsPrint = JasperFillManager.fillReport(l_jsRpt, l_params, l_jrRS);

		} catch (JRException je) {
			throw je;
		} catch (Exception e) {
			throw e;
		} finally {
			closeConnection();
		}

		return l_jsPrint;

	}
}
